package com.ruoyi.yuyuan.Event;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.yuyuan.Entity.*;
import com.ruoyi.yuyuan.Mapper.AgreementBlogMapper;
import com.ruoyi.yuyuan.Mapper.NoticeMapper;
import com.ruoyi.yuyuan.Service.IElasticSearch;
import com.ruoyi.yuyuan.Service.INoticeService;
import com.ruoyi.yuyuan.Utils.CommunityConstant;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;


@Component
public class EventConsumer implements CommunityConstant {

    private static final Logger logger = LoggerFactory.getLogger(EventConsumer.class);

    @Autowired
    private IElasticSearch elasticSearch;

    @Autowired
    private NoticeMapper noticeMapper;

    @KafkaListener(topics = {TOPIC_COMMENT, TOPIC_LIKE, TOPIC_FOLLOW})
    public void handleSendMessage(ConsumerRecord record) {
        if (record == null || record.value() == null) {
            logger.error("消息的内容为空！");
            return;
        }
        Event event = JSONObject.parseObject(record.value().toString(), Event.class);
        if (event == null) {
            logger.error("消息格式错误！");
            return;
        }
        //TODO 通知信息
        noticeMapper.insert(BeanUtil.copyProperties(event.getData(), Notice.class));

    }

    // 消费发帖事件
    @KafkaListener(topics = {TOPIC_PUBLIC})
    public void handlePublishMessage(ConsumerRecord record) {

        if (record == null || record.value() == null) {
            logger.error("消息的内容为空！");
            return;
        }
        Event event = JSONObject.parseObject(record.value().toString(), Event.class);
        if (event == null) {
            logger.error("消息格式错误！");
            return;
        }
        saveBlog(event.getData(),event.getObjectType());

    }
    // 消费更帖事件
    @KafkaListener(topics = {TOPIC_UPDATE})
    public void handleUpdateMessage(ConsumerRecord record) {

        if (record == null || record.value() == null) {
            logger.error("消息的内容为空！");
            return;
        }
        Event event = JSONObject.parseObject(record.value().toString(), Event.class);
        if (event == null) {
            logger.error("消息格式错误！");
            return;
        }
        deleteBlog(event.getData(),event.getObjectType());
        saveBlog(event.getData(),event.getObjectType());

    }
    // 消费删帖事件
    @KafkaListener(topics = {TOPIC_DELETE})
    public void handleDeleteMessage(ConsumerRecord record) {

        if (record == null || record.value() == null) {
            logger.error("消息的内容为空！");
            return;
        }
        Event event = JSONObject.parseObject(record.value().toString(), Event.class);
        if (event == null) {
            logger.error("消息格式错误！");
            return;
        }
        deleteBlog(event.getData(),event.getObjectType());

    }

    private void saveBlog(Object object, String objectType){
        switch (objectType) {
            case "BlogAgreement":
                elasticSearch.saveAgreementBlog(BeanUtil.copyProperties(object, BlogAgreement.class));
                break;
            case "BlogDeal":
                elasticSearch.saveDealBlog(BeanUtil.copyProperties(object, BlogDeal.class));
                break;
            case "BlogPractice":
                elasticSearch.savePracticeBlog(BeanUtil.copyProperties(object, BlogPractice.class));
                break;
            case "BlogDaily":
                elasticSearch.saveDailyBlog(BeanUtil.copyProperties(object, BlogDaily.class));
                break;
        }
    }
    private void deleteBlog(Object object, String objectType){
        switch (objectType) {
            case "BlogAgreement":
                elasticSearch.deleteAgreementBlog(((BlogAgreement) object).getId());
                break;
            case "BlogDeal":
                elasticSearch.deleteDealBlog(((BlogDeal) object).getId());
                break;
            case "BlogPractice":
                elasticSearch.deletePracticeBlog(((BlogPractice) object).getId());
                break;
            case "BlogDaily":
                elasticSearch.deleteDailyBlog(((BlogDaily) object).getId());
                break;
        }
    }
}
